﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Blokus.Controls
{
    public class TextBox
    {
        public string Text;
        private string header;
        public bool intOnly = false;
        private Rectangle headerRectangle;
        public string Header
        {
            get { return header; }
            set { header = value; headerRectangle = new Rectangle(TextBoxPos.X - (int)GameFont.MeasureString(header).X, TextBoxPos.Y, (int)GameFont.MeasureString(header).X, (int)GameFont.MeasureString(header).Y); }
        }
        public Color headerColor = Color.Black;
        public bool hasFocus = false;
        public Rectangle TextBoxPos;
        KeyboardState laststate;
        public SpriteBatch spriteBatch;
        public SpriteFont GameFont;
        public Texture2D backgroundTexture;
        public int padding = 0;
        public bool deleteOnFocus = false;
        public Color myTexColor = Color.White;
        public int maxCharLimit = 100;
        public bool colorOnMouseOver = false;
        public Color hoverTexColor = Color.Gray;
        public Color noHoverTexColor = Color.White;
        public bool isFocusable = true;

        private MouseState prevMouseState;

        public TextBox(string text, bool hasFocus, Rectangle TextBoxPosition)
        {
            this.Text = text;
            this.hasFocus = hasFocus;
            this.TextBoxPos = TextBoxPosition;
        }
        public bool Update(GameTime gameTime, MouseState ms)
        {
            bool changedFocus = false;

            TextBoxPos.Inflate(5 + padding, 5 + padding);
            if (ApplicationLayer.FreqUsed.isInRectangle(ms, TextBoxPos) && ms.LeftButton == ButtonState.Pressed)
            {
                hasFocus = true;
                changedFocus = true;
                if (deleteOnFocus)
                    Text = "";
            }
            else if (ms.LeftButton == ButtonState.Pressed)
            {
                hasFocus = false;
            }
            else if (ApplicationLayer.FreqUsed.isInRectangle(ms, TextBoxPos))
            {
                if (colorOnMouseOver)
                    myTexColor = hoverTexColor;
            }
            else if (!ApplicationLayer.FreqUsed.isInRectangle(ms, TextBoxPos))
            {
                if (colorOnMouseOver)
                    myTexColor = noHoverTexColor;
            }
            TextBoxPos.Inflate(-5 - padding, -5 - padding);
            if (hasFocus && isFocusable)
            {
                KeyboardState keyboardState = Keyboard.GetState();
                if (keyboardState != laststate)
                {
                    if (keyboardState.IsKeyDown(Keys.Delete) || keyboardState.IsKeyDown(Keys.Back))
                    {
                        if (Text.Length > 0)
                            Text = Text.Substring(0, Text.Length - 1);
                    }
                    foreach (Keys k in keyboardState.GetPressedKeys())
                    {
                        if (laststate.IsKeyDown(k))
                            continue;
                        if (intOnly)
                        {
                            int num;
                            string test = "";
                            if (k.ToString().Length == 2)
                            {
                                test = k.ToString().Substring(1, 1);
                            }
                            else if (k.ToString().Length == 7)
                            {
                                test = k.ToString().Substring(6, 1);
                            }
                            if (int.TryParse(test, out num))
                            {
                                Text += num;
                            }
                        }
                        else if (k.ToString().Length == 1 && char.IsLetterOrDigit(Convert.ToChar(k.ToString())))
                        {
                            if (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift))
                                Text += k.ToString();
                            else
                                Text += k.ToString().ToLower();
                        }
                        else if (k == Keys.Space)
                            Text += " ";
                        if (Text.Length > maxCharLimit)
                            Text = Text.Substring(0, maxCharLimit);
                    }
                }
            }
            laststate = Keyboard.GetState();
            bool gainedFocus = changedFocus && (isFocusable || prevMouseState.LeftButton != ButtonState.Pressed);
            prevMouseState = ms;
            return gainedFocus;
        }


        public void Draw()
        {
            if (!string.IsNullOrWhiteSpace(header))
            {
                spriteBatch.DrawString(GameFont, header, new Vector2(headerRectangle.X, headerRectangle.Y), headerColor);
            }
            TextBoxPos.Inflate(5 + padding, 5 + padding);
            spriteBatch.Draw(backgroundTexture, TextBoxPos, myTexColor);
            TextBoxPos.Inflate(-5 - padding, -5 - padding);
            string drawString = Text;
            if (hasFocus && isFocusable)
            {
                drawString += "|";
            }
            spriteBatch.DrawString(GameFont, drawString, new Vector2(TextBoxPos.X, TextBoxPos.Y), Color.Black);
        }
    }
}
